#include<iostream>

using namespace std;
typedef long long ll;
ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(!b)
    {
        x=1,y=0;
        return a;
    }
    
    ll d=exgcd(b,a%b,y,x);
    y-=a/b*x;
    return d;
}

int main()
{
    ll n;cin>>n;
    bool ans=true;
    ll a1,m1;cin>>a1>>m1;
    for(int i=0;i<n-1;++i)
    {
        ll a2,m2;cin>>a2>>m2;
        
        ll k1,k2;
        ll d=exgcd(a1,a2,k1,k2);
        
        
        if((m2-m1)%d) ans=false;
        
        k1*=(m2-m1)/d;
        
        ll t=a2/d;
        k1=(k1%t+t)%t;
        
        m1=a1*k1+m1;
        a1=abs(a1/d*a2);
    }
    
    if(ans) cout<<(m1%a1+a1)%a1<<endl;
    else printf("-1\n");
    return 0;
}